package com.wfyn.reggie.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfyn.reggie.common.web.domain.Result;
import com.wfyn.reggie.common.web.domain.ResultCode;
import com.wfyn.reggie.modules.sys.domain.Employee;
import com.wfyn.reggie.modules.sys.param.PageListRequest;
import com.wfyn.reggie.modules.sys.service.EmployeeService;
import com.wfyn.reggie.modules.sys.mapper.EmployeeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
* @author wfyn
* @description 针对表【employee(员工信息)】的数据库操作Service实现
* @createDate 2023-09-25 15:59:00
*/
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{
    @Autowired
    HttpServletRequest request;
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public Result<Employee> login(Employee employee) {
        //1.密码利用m5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //2.根据用户名查询用户
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = getOne(queryWrapper);
        //3.用户对比
        if (emp == null) {
            return Result.failure(ResultCode.USER_USERNAME_NOT_FOUND);
        }

        //4.密码对比
        if (!emp.getPassword().equals(password)) {
            return Result.failure(ResultCode.USER_BAD_CREDENTIALS);
        }
        //5.检查用户是否启用
        if (emp.getStatus() == 0) {

            return Result.failure(ResultCode.USER_NOT_ENABLE);
        }
        //6.登录成功
        request.getSession().setAttribute("employee",emp.getId());

        System.out.println(request.getSession().getId());
        return Result.success(emp);
    }

    @Override
    public Result<String> logout() {
        //销毁session
        request.getSession().removeAttribute("employee");

        return  Result.success(ResultCode.LOGOUT_SUCCESS);
    }

    @Override
    public Result<String> saveEmployee(Employee employee) {
        log.info("新增员工，员工信息：{}", employee);
        //设置初始密码为123456,并m5进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());

        employee.setUpdateTime(LocalDateTime.now());

        //获取当前用户的id
        Long empId = (Long) request.getSession().getAttribute("employee");

        employee.setUpdateUser(empId);
        employee.setCreateUser(empId);
        save(employee);


        return Result.success("新增员工成功");
    }

    @Override
    public Result<Page<Employee>> pageList(PageListRequest pageListRequest) {

        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(pageListRequest.getPage(), pageListRequest.getPageSize());
        //构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(pageListRequest.getName()),Employee::getName,pageListRequest.getName());
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Employee::getId);
        //执行查询
        page(pageInfo, lambdaQueryWrapper);
        return  Result.success(pageInfo);

    }

    @Override
    public Result<String> updateEmployee(Employee employee) {
        //获得当前登录用户的Id
        Long empId = (Long)request.getSession().getAttribute("employee");

        employee.setUpdateUser(empId);
        employee.setUpdateTime(LocalDateTime.now());

        updateById(employee);
        return Result.success("更新成功");
    }

    @Override
    public Result<Employee> getEmployeeById(Integer id) {


        Employee employee = employeeMapper.selectById(id);

        return Result.success(employee);
    }
}




